//
//  CinemaViewController.m
//  MyMovie
//
//  Created by Weiwei on 16/2/24.
//  Copyright © 2016年 bever贝沃汇力 weiwei. All rights reserved.
//

#import "CinemaViewController.h"
#import "CinemaCell.h"
#import "CinemaModel.h"
#import "TipView.h"
@interface CinemaViewController (){
    
    SegmentView* _segMentView;
    NSMutableArray* _dataList;
    UITableView* _tableView;
    NSMutableArray* _oldDataList;//和_datalIst同时接受model，用来还原
    TipView* _tipView;
    
}

@end

@implementation CinemaViewController

//重写init方法，在viewC被创建的时候就给他的navigationItem和tabBarItem
-(id)init{
    if (self = [super init]) {
        
        
        self.navigationItem.title = @"影院";
        self.tabBarItem.image = [UIImage imageNamed:@"index_menu_ciname"];
        
        
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self loadData];
    [self createSegmentView];
    [self createTableView];
    
#pragma -mark 创建一个tipview
    [self createTipView];
    
    
    
}


-(void)createTipView{
    
    
    _tipView  = [[[NSBundle mainBundle]loadNibNamed:@"TipView" owner:nil options:nil]lastObject];
    
    _tipView.frame = CGRectMake(0,KScreenH, KScreenW, 200);
    [self.view addSubview:_tipView];
    
    
}






-(void)loadData{
    
    
    _dataList = [[NSMutableArray alloc]initWithCapacity:1];
     _oldDataList = [[NSMutableArray alloc]initWithCapacity:1];
    
    //1.解析类
    NSArray* cinemaArray =[DataParser jsonObjectFromFileName:@"cinema"];
    
    //2.循环放到dataList
    for (NSDictionary* dic in cinemaArray) {
        
        CinemaModel* model = [[CinemaModel alloc]initWithDic:dic];
        [_dataList addObject:model];
        [_oldDataList addObject:model];
        
        
    }
    
    
    
    
}



-(void)createSegmentView{
    
    _segMentView = [[SegmentView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, 50) withTitleArray:@[@"全部",@"附近",@"价格",@"筛选"]];
    
    //-------------------------------
    __weak CinemaViewController* weakSelf = self;
    
    [_segMentView setSegmentBlock:^(NSInteger index) {
        
        //当segmentView被点几会调用block方法
#pragma -mark 步骤一 封装一个方法，通过index决定排序的操作
        
        __strong CinemaViewController* strongSelf = weakSelf;
        
        [strongSelf touchUp:index];
        
        
    }];
    
    
    
    [self.view addSubview:_segMentView];
    
    
    
}
#pragma -mark segment的处理事件
-(void)touchUp:(NSInteger)index{
    
    if(index == 0){
        [self old];
        
    }else if (index == 1){
        
        [self distance];
        
    }else if (index == 2){
        [self minPrice];
        
    }else if(index == 3){
        [self showTipView];
        
    }
    
}

-(void)showTipView{
    
    [UIView animateWithDuration:.3 animations:^{
        _tipView.transform = CGAffineTransformMakeTranslation(0, -200-49-64);
    }];
    
    

    
    
}


//对dataList重新加载原来的数据顺序
-(void)old{
    
    //    _dataList = _oldDataList;
    _dataList = [NSMutableArray arrayWithArray:_oldDataList];
    [_tableView reloadData];
    
    [UIView animateWithDuration:.3 animations:^{
        _tipView.transform = CGAffineTransformIdentity;
    }];
    
    
}

//按照距离排序
-(void)distance{
    
    //本地经纬度----我们自己写死一个经纬度
    float longitude = 116.767;
    float latitude = 36.55;
    
    
    
    //冒泡法---
    
    //一次排序选出最大的
    
    for (int j = (int)_dataList.count -1; j>0; j--) {
        // 0 1 2 3 4  _dataListCont = 5
        for (int i = 0; i < j; i++) {
            //获得两个model
            CinemaModel* model1 = _dataList[i];
            CinemaModel* model2 = _dataList[i+1];
            
            //------唯一需要改的是判断的条件----------
            //给model增加两个属性---经纬度
            
            //获得model传递的经纬度，算出和我们本地的经纬的茬子。求出平方，平方大的，距离大
            
            //1.获得Model1距离本地的距离大小
            CGFloat dis_long =  [model1.longitude floatValue] - longitude;
            CGFloat dis_lat  =  [model1.latitude floatValue] - latitude;
            //算出平方和
            CGFloat dis1 = pow(dis_long, 2) + pow(dis_lat, 2);
            
            //2.model2的距离计算
            CGFloat dis_long2 =  [model2.longitude floatValue] - longitude;
            CGFloat dis_lat2  =  [model2.latitude floatValue] - latitude;
            //算出平方和
            CGFloat dis2 = pow(dis_long2, 2) + pow(dis_lat2, 2);
            
            
            if (dis1 > dis2) {
                [_dataList exchangeObjectAtIndex:i withObjectAtIndex:i+1];
                
            }
            
        }
        
    }
    
    
    [_tableView reloadData];
    [UIView animateWithDuration:.3 animations:^{
        _tipView.transform = CGAffineTransformIdentity;
    }];
    
}





//按照价格顺序排序
-(void)minPrice{
    NSLog(@"按照价格排序");
    
    
    //冒泡法---
    
    //一次排序选出最大的
    
    for (int j = (int)_dataList.count -1; j>0; j--) {
        // 0 1 2 3 4  _dataListCont = 5
        for (int i = 0; i < j; i++) {
            //获得两个model
            CinemaModel* model1 = _dataList[i];
            CinemaModel* model2 = _dataList[i+1];
            
            if ([model1.minPrice integerValue] > [model2.minPrice integerValue]) {
                
                //如果第一个大于第二个就交换两个位置
                [_dataList exchangeObjectAtIndex:i withObjectAtIndex:i+1];
            }
            
            
        }
        
    }
    
    [_tableView reloadData];
    
    [UIView animateWithDuration:.3 animations:^{
        _tipView.transform = CGAffineTransformIdentity;
    }];
    
    
    
    
}





-(void)createTableView{
    
    //1.创建一个tablevIEW
    
//    UITableView* _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _segMentView.frame.origin.y+ _segMentView.frame.size.height, <#CGFloat width#>, <#CGFloat height#>) style:UITableViewStylePlain];
    
    //1.引入第三方插件
   _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _segMentView.bottom, KScreenW, KScreenH- 64 -49 -_segMentView.height) style:UITableViewStylePlain];
    //2.导入代理
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    _tableView.backgroundColor = [UIColor clearColor];
    
    [self.view addSubview:_tableView];
    
    
    

}

#pragma -mark 实现代理和数据源协议
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataList.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    CinemaCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cinema_cell"];
    
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"CinemaCell" owner:nil options:nil]lastObject];
        cell.backgroundColor = [UIColor clearColor];
    }
    
    //获得当前数据
     CinemaModel* model = _dataList[indexPath.row];
     cell.model = model;
    
    
    return cell;
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 120;
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
